{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Intro\n",
    "Notebook that explores with examples advanced Python topics like closures, decorators, generators, etc."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Decorator\n",
    "\"a callable that takes a function as an argument and returns a replacement function\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:26:33.242845",
     "start_time": "2017-08-02T19:26:33.239845"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import functools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:28:55.569986",
     "start_time": "2017-08-02T19:28:55.553985"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# decorator function, which does basic logging\n",
    "def log(fun):\n",
    "    @functools.wraps(fun)\n",
    "    def wrapper(*args, **kw):\n",
    "        print(\"Before decoration\")\n",
    "        result = fun(*args, **kw)\n",
    "        print(\"After invocation\")\n",
    "        return result\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:29:14.420064",
     "start_time": "2017-08-02T19:29:14.411064"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# base function\n",
    "@log\n",
    "def sum(a, b):\n",
    "    return a + b\n",
    "\n",
    "# equivalent to\n",
    "#sum = log(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:29:15.542128",
     "start_time": "2017-08-02T19:29:15.520127"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before decoration\n",
      "After invocation\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(1,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mutable Default Argument Value\n",
    "What will the following code print?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-03T09:49:33.907458",
     "start_time": "2017-08-03T09:49:33.896458"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def test(n, l=[]):\n",
    "    for i in range(n):\n",
    "        l.append(i)\n",
    "    print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-03T09:49:34.092469",
     "start_time": "2017-08-03T09:49:34.083469"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2]\n",
      "[1, 2, 3, 0, 1, 2]\n",
      "[0, 1, 2, 0, 1, 2]\n"
     ]
    }
   ],
   "source": [
    "test(3)\n",
    "test(3, [1,2,3])\n",
    "test(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-03T09:54:18.175718",
     "start_time": "2017-08-03T09:54:18.163717"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Signature (n, l=[0, 1, 2, 0, 1, 2])>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# it can be noticed that the mutable argument changes\n",
    "# are memorized in the method signature\n",
    "import inspect\n",
    "inspect.signature(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:kaggle]",
   "language": "python",
   "name": "conda-env-kaggle-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "67px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
